<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/app.php');

need_manager();
need_auth('order');

$t_con = array(
	
);
$teams = DB::LimitQuery('team', array('condition'=>$t_con));
$t_id = Utility::GetColumn($teams, 'id');

$condition = array(
	'team_id' => $t_id,
	'team_id > 0',
);


if(isset($_GET['commission_id']) && $_GET['commission_id']>0)
{
	$condition['commission_id'] = intval($_GET['commission_id']);	
}

if(!is_manager(1))
{
	$partner = DB::LimitQuery('partner', array(
		'condition' => array('user_id'=>$login_user['id']),
		'one' => true,
	));
	$partner_id = $partner['id'];
	$condition['partner_id'] = $partner_id;
	//city_id
}

/* filter */
$uemail = strval($_GET['uemail']);
if ($uemail) {
	$field = strpos($uemail, '@') ? 'email' : 'username';
	//$field = is_numeric($uemail) ? 'id' : $field;
	$uuser = Table::Fetch('user', $uemail, $field);
	if($uuser) $condition['user_id'] = $uuser['id'];
	else $uemail = null;
}
$id = abs(intval($_GET['id'])); 
if ($id) 
	$condition['id'] = $id;
else 
	$id = '';
$team_id = abs(intval($_GET['team_id']));
if ($team_id && in_array($team_id, $t_id)) {
	$condition['team_id'] = $team_id;
} else { $team_id = null; }

$cbday = strval($_GET['cbday']);
$ceday = strval($_GET['ceday']);
$pbday = strval($_GET['pbday']);
$peday = strval($_GET['peday']);

$is_fahuo = isset($_GET['is_fahuo']) ? intval($_GET['is_fahuo']) : 0;

if($is_fahuo >0)
{
	//is_fahuo = 1  is_fahuo =2
	//$one['express_no']&&$one['express_id']
	if($is_fahuo == 1){
		$condition[] = "express_no !='' and express_id !='' ";
	} else if($is_fahuo == 2){
		$condition[] = "express_no is null ";
	}

}
if ($cbday) { 
	$cbtime = strtotime($cbday);
	$condition[] = "create_time >= '{$cbtime}'";
}
if ($ceday) { 
	$cetime = strtotime($ceday);
	$condition[] = "create_time <= '{$cetime}'";
}
if ($pbday) { 
	$pbtime = strtotime($pbday);
	$condition[] = "pay_time >= '{$pbtime}'";
}
if ($peday) { 
	$petime = strtotime($peday);
	$condition[] = "pay_time <= '{$petime}'";
}
/* end fiter */

$count = Table::Count('order', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 20);

$orders = DB::LimitQuery('order', array(
	'condition' => $condition,
	'order' => 'ORDER BY id DESC',
	'size' => $pagesize,
	'offset' => $offset,
));

foreach($orders as $key =>$val)
{
	if($val['is_pin'] == 1)
	{
		//pin_order_list
		$pin_order_list = DB::LimitQuery('pin_order_list', array(
			'condition' => array('order_id'=>$val['id']),
			'one' => true,
		));
		$pin_order =  DB::LimitQuery('pin_order', array(
			'condition' => array('id'=>$pin_order_list['pin_id']),
			'one' => true,
		));
		$val['pin_info'] = $pin_order;
		
		$find_pin_order_list = DB::LimitQuery('pin_order_list', array(
			'condition' => array('pin_id'=>$pin_order_list['pin_id']),
			'order' => 'ORDER BY createtime ASC',
			'one' => true,
		));
		
		$tmp_order = Table::Fetch('order', $find_pin_order_list['order_id']);	
		$tmp_user = Table::Fetch('user', $tmp_order['user_id']);	
		
		//id val  user_id 
		if($tmp_user['id'] == $val['user_id']){
			$val['is_tuanz'] = true;
		} else {
			$val['is_tuanz'] = false;
			$val['tuan_info'] = $tmp_user;
			$val['tuan_order_id'] = $find_pin_order_list['order_id'];
		}
		//var_dump($tmp_user);die();
		
		$orders[$key] = $val;
	}
}

$pay_ids = Utility::GetColumn($orders, 'pay_id');
$pays = Table::Fetch('pay', $pay_ids);

$user_ids = Utility::GetColumn($orders, 'user_id');
$users = Table::Fetch('user', $user_ids);

$team_ids = Utility::GetColumn($orders, 'team_id');
$teams = Table::Fetch('team', $team_ids);

include template('manage_order_index');
